/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import binding.carts.Order;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.CommonUtils;
import utils.XmlUtils;

public class FlowController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            String webPath = getServletContext().getRealPath("/");

            if (action.equals("TRANSFORM_FILTER_COMMENTS_BY_PRODUCT")) {
                Map<String, Object> params = new LinkedHashMap<String, Object>();
                String proid = request.getParameter("proid");
                params.put("proid", proid);
                params.put("propath", webPath + "WEB-INF/XmlDoc/Products.xml");
                params.put("userpath", webPath + "WEB-INF/XmlDoc/Users.xml");
                params.put("userdetailpath", webPath + "WEB-INF/XmlDoc/UserDetails.xml");
                params.put("type", "control");
                String result = XmlUtils.transformToString(webPath + "WEB-INF/XslDoc/Comment.xsl", webPath + "WEB-INF/XmlDoc/Comments.xml", params);
                out.write(result);
            } else if (action.equals("TRANSFORM_FILTER_SAVE_BY_PRODUCT")) {
                Map<String, Object> params = new LinkedHashMap<String, Object>();
                params.put("type", "saveproduct");
                String result = XmlUtils.transformToString(webPath + "WEB-INF/XslDoc/Management.xsl", webPath + "WEB-INF/XmlDoc/Products.xml", params);
                out.write(result);
            } else if (action.equals("TRANSFORM_ORDERDETAIL_ADMINPAGE")) {
                Map<String, Object> params = new LinkedHashMap<String, Object>();
                String orderid = request.getParameter("orderid");
                params.put("orderidresponse", CommonUtils.convertStringToInt(orderid));
                params.put("orderpath", webPath + "WEB-INF/XmlDoc/Orders.xml");
                params.put("userpath", webPath + "WEB-INF/XmlDoc/Users.xml");
                params.put("userdetailpath", webPath + "WEB-INF/XmlDoc/UserDetails.xml");
                params.put("propath", webPath + "WEB-INF/XmlDoc/Products.xml");
                params.put("type", "inorderdetail");
                String result = XmlUtils.transformToString(webPath + "WEB-INF/XslDoc/Order.xsl", webPath + "WEB-INF/XmlDoc/OrderDetails.xml", params);
                out.write(result);
            } else if (action.equals("TRANSFORM_ORDER_ON_ADMIN_PAGE")) {
                Map<String, Object> params = new LinkedHashMap<String, Object>();
                params.put("userpath", webPath + "WEB-INF/XmlDoc/Users.xml");
                params.put("userdetailpath", webPath + "WEB-INF/XmlDoc/UserDetails.xml");
                params.put("type", "inorder");
                String result = XmlUtils.transformToString(webPath + "WEB-INF/XslDoc/Order.xsl", webPath + "WEB-INF/XmlDoc/Orders.xml", params);
                out.write(result);
            } else if (action.equals("TRANSFORM_USER_DETAIL")) {
                Map<String, Object> params = new LinkedHashMap<String, Object>();
                String userdetailid = request.getParameter("userdetailid");

                params.put("type", "adminview");
                params.put("userdetailid", CommonUtils.convertStringToInt(userdetailid));

                String result = XmlUtils.transformToString(webPath + "WEB-INF/XslDoc/UserDetails.xsl", webPath + "WEB-INF/XmlDoc/UserDetails.xml", params);
                out.write(result);
            } else if (action.equals("REQUEST_CART")) {
                HttpSession session = request.getSession();
                String cart = "";
                Object cartdoc = session.getAttribute("CART_DOC");
                if (cartdoc != null) {
                    cart = (String) cartdoc;
                } else {
                    //create new order info
                    Order order = new Order();
                    order.setUserId(BigInteger.ZERO);
                    order.setCount(BigInteger.ZERO);
                    cart = XmlUtils.marshalXml(order);
                }
                response.setContentType("text/xml");
                out.write(cart);
            } else if (action.equals("SAVE_ORDER_TO_SESSION")) {
                String content = request.getParameter("content");
                HttpSession session = request.getSession();
                session.setAttribute("CART_DOC", content);
            } else if (action.equals("HAVE_USER_DETAIL")) {
                Object obj = request.getSession().getAttribute("USER_DETAIL_ID");
                String message = "false";
                if (obj != null) {
                    message = "true";
                }
                out.write(message);
            } else if (action.equals("TRANSFORM_COMMENT")) {
                Map<String, Object> params = new LinkedHashMap<String, Object>();
                String productid = request.getParameter("proid");

                params.put("type", "COMMENT_LOAD");
                params.put("proid", CommonUtils.convertStringToInt(productid));
                params.put("propath", webPath + "WEB-INF/XmlDoc/Products.xml");
                params.put("userpath", webPath + "WEB-INF/XmlDoc/Users.xml");
                params.put("userdetailpath", webPath + "WEB-INF/XmlDoc/UserDetails.xml");
                String result = XmlUtils.transformToString(webPath + "WEB-INF/XslDoc/Comment.xsl", webPath + "WEB-INF/XmlDoc/Comments.xml", params);
                out.write(result);
            }

        } catch (Exception ex) {
            Logger.getLogger(FlowController.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
